package com.jwthhealth_pub.old.diet.view;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.constitution.ConstitutionTypeUtil;
import com.jwthhealth_pub.old.diet.moudel.DietThiModule;
import com.jwthhealth_pub.old.diet.view.adapter.DietthiAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/12/27.
 */
public class DietThiActivity extends Activity {

    public static int TOAS = 0;
    public static int LOAD = 1;

    @BindView(R.id.rb)
    RecyclerView rv;
    @BindView(R.id.green)
    RelativeLayout view;
    @BindView(R.id.type)
    TextView type;
    @BindView(R.id.sign_topbar)
    TitleLayout signTopbar;
    //    @BindView(R.id.SwipeRefreshLayout)
//    android.support.v4.widget.SwipeRefreshLayout swipeRefreshLayout;
    private DietthiAdapter adapter;
    private GridLayoutManager layout;

    boolean isLoading;
    private List<DietThiModule.DataBean> data;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == TOAS) {
                Toast.makeText(DietThiActivity.this, msg.getData().getString(Constant.DIETDATA), Toast.LENGTH_SHORT).show();
            } else {
                DietThiModule module = (DietThiModule) msg.getData().getSerializable(Constant.DIETDATA);
                List<DietThiModule.DataBean> tempData = module.getData();
                for (int i = 0; i < tempData.size(); i++) {
                    data.add(tempData.get(i));
                }
                adapter.notifyDataSetChanged();
                isLoading = false;
            }

        }
    };
    private String topid;
    private String id;
    private String sonId;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diet_thi);
        ButterKnife.bind(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        initData();
        initTopBar();
        initRecycleView();
    }

    private void initData() {
        Bundle bundle = getIntent().getExtras();
        DietThiModule module = (DietThiModule) bundle.getSerializable(Constant.DIETDATA);
        data = module.getData();
        topid = bundle.getString(Constant.TOPID);
        id = bundle.getString(Constant.ID);
        sonId = bundle.getString(Constant.SONID);
        type.setText("分类：" + bundle.getString(Constant.TYPENAME));
    }

    private void initRecycleView() {
        layout = new GridLayoutManager(this, 3);
        layout.setSmoothScrollbarEnabled(false);
        rv.setLayoutManager(layout);
        adapter = new DietthiAdapter(this, data);
        adapter.setTitle(title);
        rv.setAdapter(adapter);

        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                Log.d("test", "StateChanged = " + newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                int lastVisibleItemPosition = layout.findLastVisibleItemPosition();
                if (lastVisibleItemPosition + 1 == adapter.getItemCount()) {

                    if (!isLoading) {
                        loadMore();
                    }
                }
            }
        });

    }

    private void loadMore() {
        isLoading = true;
        String page = String.valueOf(Integer.valueOf(data.get(data.size() - 1).getId()) + 1);
        Call<DietThiModule> call = ApiHelper.dietRequestThiInfo(topid, id, sonId, page);
        call.enqueue(new Callback<DietThiModule>() {
            @Override
            public void onResponse(Call<DietThiModule> call, Response<DietThiModule> response) {
                DietThiModule module = response.body();
                if (module.getCode().equals("0")) {
                    Message message = new Message();
                    message.what = LOAD;
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(Constant.DIETDATA, module);
                    message.setData(bundle);
                    handler.sendMessage(message);
                } else {
                    Message message = new Message();
                    message.what = TOAS;
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.DIETDATA, module.getMsg());
                    message.setData(bundle);
                    Log.d("IDietPresneterComple", "fail");
                    handler.sendMessage(message);
                }
            }

            @Override
            public void onFailure(Call<DietThiModule> call, Throwable t) {

            }
        });
    }

    private void initTopBar() {
        title = ConstitutionTypeUtil.GetDietTitle(topid, id);
        signTopbar.setTitle(title);
        signTopbar.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }
}
